package com.liesheng.haylou.ui.personal;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.UpdatePswBean;
import com.liesheng.haylou.databinding.ActivityAlterPsdBinding;
import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.net.HttpRequest;
import com.liesheng.haylou.ui.reset.ResetPswActivity;
import com.liesheng.haylou.utils.FireGsonUtil;
import com.liesheng.haylou.utils.ToastUtil;
import com.liesheng.haylou.utils.sp.SpUtil;

import java.util.HashMap;
import java.util.Map;

import androidx.databinding.DataBindingUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AlterPasswordActivity extends BaseActivity<ActivityAlterPsdBinding, BaseVm> {
    String oldPassword;
    String newPassword;

    @Override
    protected ActivityAlterPsdBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_alter_psd, null, false);
        //隐藏掉整个ActionBar
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        setTitle("更改密码");

    }

    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, AlterPasswordActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.affirmRescan:
                UpdatePsw();
                break;
            case R.id.tvForgetOldPsw:
                ResetPswActivity.startBy(this);
                break;
        }
    }

    private void UpdatePsw() {
        oldPassword = mBinding.etOldPsw.getText().toString();
        newPassword = mBinding.etNewPsw.getText().toString();
        mBinding.etAffirmPsw.getText().toString();
        if (TextUtils.isEmpty(oldPassword)) {
            ToastUtil.showToast("请输入旧密码！");
            return;
        }
        if (TextUtils.isEmpty(newPassword)) {
            ToastUtil.showToast("请输入新密码！");
            return;
        }
        if (TextUtils.isEmpty(mBinding.etAffirmPsw.getText().toString())) {
            ToastUtil.showToast("请再次确认新密码！");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        requestHttp(buildHttpService().getPasswordUpdate(HttpRequest.getBody(map)), new HttpCallback<UpdatePswBean>() {
            @Override
            public void onNext(UpdatePswBean updatePswBean) {
                if (updatePswBean != null) {
                    updatePswBean.getMsg();
                }
            }
        });
    }

}
